import { isBlank, isObject } from './typeUitl'
import axios from 'axios'
import moment from 'moment'
/**
 * 向url中添加参数
 * @param url
 * @param params 要添加的参数对象 {参数名: 参数值}
 * @param isUrl 是否自动判断增加问号 默认true
 * @return {*} 拼接好的url
 */
export function addUrlParams(url, params, isUrl) {
    if (isUrl !== false && url.indexOf('?') === -1) {
        url += '?'
    }
    for (const item in params) {
        let value = params[item]
        if (isBlank(value)) {
            value = ''
        }
        if (isObject(value)) {
            value = JSON.stringify(value)
        }
        if (!url || (url.substr(url.length - 1) !== '&' && url.substr(url.length - 1) !== '?')) {
            url += '&'
        }
        url += decodeURIComponent(item) + '=' + encodeURIComponent(value)
    }
    return url
}

export function baseAddUrlParams(url, params, isUrl) {
    if (isUrl !== false && url.indexOf('?') === -1) {
        url += '?'
    }
    let pastr = ''
    for (const item in params) {
        let value = params[item]
        if (isBlank(value)) {
            value = ''
        }
        if (isObject(value)) {
            value = JSON.stringify(value)
        }
        if (
            !pastr ||
            (pastr.substr(pastr.length - 1) !== '&' && pastr.substr(pastr.length - 1) !== '?')
        ) {
            pastr += '&'
        }
        pastr += `${item}=${value}`
    }
    return url + window.btoa(pastr)
}

export function addExeParams(url, params) {
    let reUrl = `${url}://`
    for (const item in params) {
        reUrl += `${item}=${params[item]}&`
    }
    reUrl = reUrl.substring(0, reUrl.length - 1)
    return reUrl
}

/**
 * 解析url中拼接的参数
 * @param url
 * @return {{参数名: string}}
 */
export function parseUrlParams(url) {
    if (url.indexOf('?') !== -1) {
        url = url.substr(url.indexOf('?') + 1)
    }
    const paramsArr = url.match(/[^\?\=\&]*\=[^\?\=\&]*/g)
    let params = {}
    if (paramsArr != null) {
        paramsArr.forEach(item => {
            const kv = item.split('=')
            params[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1]).replace('%20', ' ')
        })
    }
    return params
}

/**
 * 解析url中拼接的参数
 * @param url
 * @return {{参数名: string}}
 */
export function baseParseUrlParams(url) {
    if (url.indexOf('?') !== -1) {
        url = url.substr(url.indexOf('?') + 1)
        const paramsArr = window.atob(url).match(/[^\?\=\&]*\=[^\?\=\&]*/g)
        let params = {}
        if (paramsArr != null) {
            paramsArr.forEach(item => {
                const kv = item.split('=')
                params[kv[0]] = kv[1]
            })
        }
        return params
    } else {
        return {}
    }
}

export function exportExcel(api, filelName = '下载文件', type = 'excel') {
    axios
        .get(addUrlParams(process.env.REACT_APP_SETTING_IP + api, {}), {
            responseType: 'blob',
        })
        .then(res => {
            let data = res.data
            let url = window.URL.createObjectURL(
                new Blob([data], {
                    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                })
            )
            let currentTime = moment().format('YYYY-MM-DD HH:mm:ss')
            let a = document.createElement('a')
            a.href = url
            a.download = `${filelName}-${currentTime}.${type}`
            a.click()
            // 5.释放这个临时的对象url
            window.URL.revokeObjectURL(url)
        })
}
